有时候我们需要区分一堆 List 的 changeset。特别是当这个 List 可以被增加可以被修改可以被删除的时候。
需要获得以下几个 List

  • added_list
  • updated_list
  • deleted_list
  • unchanged_list

写了一个 JS 实现。
入参是四个 original, current, getKeyFunction, checkUpdatedFunction
前两个没什么好说了。

  • getKeyFunction 默认是返回入参本身。如果入参 List 是 Object 可以返回这个 Object 的 Oid 之类的值。
  • checkUpdatedFunction 是比对是否有修改过的方法。默认比对两个 Object 的 JSON 值。

砖头还是轻轻的来比较好 ……

function getChangeSet(original, current, getKeyFunction, checkUpdatedFunction) {
    original = [].concat(original);
    current = [].concat(current);
    if (getKeyFunction == undefined) {
        getKeyFunction = function(item) {
            return item;
        }
    }
    if (checkUpdatedFunction == undefined) {
        checkUpdatedFunction = function(obj1, obj2) {
            return JSON.stringify(obj1) != JSON.stringify(obj2);
        }
    }
    var listToMap = function(list) {
        var ret = {};
        for (var i = 0, l = list.length; i < l; i++) {
            ret[getKeyFunction(list[i])] = list[i];
        }
        return ret;
    };
    var getValueListOfMap = function(map) {
        var ret = [];
        for (var key in map) {
            if (map.hasOwnProperty(key)) {
                ret.push(map[key]);
            }
        }
        return ret;
    };
    var getKeyListOfMap = function(map) {
        var ret = [];
        for (var key in map) {
            if (map.hasOwnProperty(key)) {
                ret.push(key);
            }
        }
        return ret;
    };
    var weightMap = {},
        calWeight = function(list, weight, weightMap) {
            for (var i = 0, l = list.length; i < l; i++) {
                var key = list[i];
                if (typeof weightMap[key] === "number") {
                    weightMap[key] += weight;
                } else {
                    weightMap[key] = weight;
                }
            }
        }
    var added = [],
        updated = [],
        deleted = [],
        unchanged = [];
    var oriMap = listToMap(original),
        curMap = listToMap(current),
        oriKeys = getKeyListOfMap(oriMap),
        curKeys = getKeyListOfMap(curMap);
    calWeight(oriKeys, 1, weightMap);
    calWeight(curKeys, 2, weightMap);
    for (var key in weightMap) {
        if (weightMap.hasOwnProperty(key)) {
            var value = weightMap[key];
            if (value == 3) {
                if (checkUpdatedFunction(oriMap[key], curMap[key])) {
                    updated.push(curMap[key]);
                } else {
                    unchanged.push(oriMap[key]);
                }
            } else if (value == 2) {
                added.push(curMap[key]);
            } else if (value == 1) {
                deleted.push(oriMap[key]);
            }
        }
    }
    return {
        added: added,
        updated: updated,
        deleted: deleted,
        unchanged: unchanged
    };
}

以下 tests 通过了

test1 = function() {
    var ori = ['t1', 't2'];
    var cur = ['t1', 't3'];
    return getChangeSet(ori, cur)
}
test2 = function() {
    var ori = [{
        key: 't1',
        value: 'v1'
    }, {
        key: 't2',
        value: 'v2'
    }, {
        key: 't4',
        value: 'v4'
    }];
    var cur = [{
        key: 't1',
        value: 'vx'
    }, {
        key: 't3',
        value: 'v3'
    }, {
        key: 't4',
        value: 'v4'
    }];
    return getChangeSet(ori, cur,
        function(item) {
            return item.key;
        })
}

liyinkan
18 声望0 粉丝

« 上一篇
year++